// 验证微信登录token
const {tokenERR}=require('../errType/err.type')
const jwt = require("jsonwebtoken");
const {selectOpenId} = require("../service/wx.user.server")
const wxPermission=async (ctx,next)=>{
    // console.log(ctx.request.header)
    try{
        const token=ctx.request.header['access-token']
        var {openId}=jwt.verify(JSON.parse(token),'zddata')
        // console.log(openId,321)
        const res=await selectOpenId(openId)
        console.log(res[0].dataValues.id,123)
        ctx.state.wxID=res[0].dataValues.id

        next()
    }catch (e) {
        switch (e.name){
            case 'TokenExpiredError':
                return ctx.app.emit('err',tokenERR ,ctx)
            default:
                return ctx.app.emit('err',tokenERR ,ctx)
        }
    }
}
module.exports = {
    wxPermission
}